package com.example.webflux;

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

/**
 * @description:
 * @projectName:WebFlux
 * @see:com.example.webflux
 * @author:LXC
 * @createTime:2024/12/17 22:33
 * @version:1.0
 */
public class WebFluxDataFlowExample {
    public static void main(String[] args) {
        // 模拟异步数据流：用户信息
        Flux<User> userFlux = Flux.just(
                new User("Alice", 30),
                new User("Bob", 25),
                new User("Charlie", 35)
        );

        // 数据流处理：转换、过滤、背压处理
        userFlux
                .map(user -> user.getName().toUpperCase()) // 转换为大写
                .filter(name -> name.startsWith("A")) // 过滤掉不以 A 开头的名字
                .doOnNext(name -> System.out.println("Processing: " + name)) // 调试日志
                .subscribeOn(Schedulers.boundedElastic()) // 使用线程池
                .doFinally(signalType -> System.out.println("Process completed")) // 最终操作
                .subscribe(System.out::println); // 打印最终结果
    }

    static class User {
        private String name;
        private int age;

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }
    }
}
